<?php echo $javascript->link('filter_actions'); ?>
<div class="stats index">
    <h2><?php __('Estadística General');?></h2>
    <a href="" class="filtro_a" onclick="show_filter(); return false;"><?php echo __('Filtro', true); ?></a>
    <input type="hidden" id="input_filtro" value="0">
    <div id="filtro_busqueda" class="filtro" style="display: block">
        <fieldset>
            <?php echo $form->create('Stat', array('url' => array('controller' => 'stats', 'action' => 'general'))); ?>
            <?php
            echo $this->Form->input('pelotero_id', array('label' => 'Particular o Pelotero', 'empty' => 'Ambos', 'options'=>array(1=>'Particular', 2=>'Pelotero')));
            ?>
            <div class="date_range">
                <label>Rango de Fechas</label>
                <?php
                echo $form->input('date_from', array('label' => false));
                ?>
                <span>-</span>
                <?php
                echo $form->input('date_to', array('label' => false));
                ?>
                <span><?php echo $this->Html->link(__('Hoy', true), array(), array('onclick' => 'fecha_hoy(); return false;')); ?></span>
                &nbsp;&nbsp;&nbsp;
                <span><?php echo $this->Html->link(__('Últimos 7 Días', true), array(), array('onclick' => 'fecha_semana(); return false;')); ?></span>
                <?php
                echo $form->input('hidden_actual_date', array('type' => 'hidden', 'value' => date('d/m/Y')));
                echo $form->input('hidden_week_date', array('type' => 'hidden', 'value' => date('d/m/Y', strtotime('-7 day', strtotime(date('Y-m-d H:i:s'))))));
                ?> 
            </div>
            <?php
            echo $form->submit(__('Filtrar', true));
            echo $form->end();
            ?>
        </fieldset>
    </div>
    <?php
    $date_from_format = '';
    $date_to_format = '';
    $particular = '';
    if(isset($this->data['Stat']['date_from'])&& !empty($this->data['Stat']['date_from'])) {
        list($d, $m, $a) = explode("/", $this->data['Stat']['date_from']);
        $date_from_format = $a . "-" . $m . "-" . $d;
    }
    if(isset($this->data['Stat']['date_to'])&& !empty($this->data['Stat']['date_to'])) {
        list($d, $m, $a) = explode("/", $this->data['Stat']['date_to']);
        $date_to_format = $a . "-" . $m . "-" . $d;
    }
    
    if(isset($this->data['Stat']['particular'])&& !empty($this->data['Stat']['particular'])) {
        $particular = $this->data['Stat']['particular'];
    }
    
    ?>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php echo '';?></th>
            <th><?php echo 'Cantidad';?></th>
            <th><?php echo 'Gasto Empleados';?></th>
            <th><?php echo 'Gasto Servicios';?></th>
            <th><?php echo 'Gasto';?></th>
            <th><?php echo 'Costo Total';?></th>
            <th><?php echo 'Ganancia Festilu';?></th>
            <th class="actions"><?php __('Acciones');?></th>
        </tr>
        <tr class="altrow">
            <td><b>Cumpleaños</b></td>
            <td><?php echo $stat_birthday[0][0]['cantidad']!=NULL?$stat_birthday[0][0]['cantidad']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_birthday[0][0]['employee_cost']!=NULL?$stat_birthday[0][0]['employee_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_birthday[0][0]['services_cost']!=NULL?$stat_birthday[0][0]['services_cost']:0; ?>&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>$<?php echo $stat_birthday[0][0]['client_cost']!=NULL?$stat_birthday[0][0]['client_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_birthday[0][0]['festilu_profit']!=NULL?$stat_birthday[0][0]['festilu_profit']:0; ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller'=>'jobs','action' => 'index', 'birthday', $date_from_format, $date_to_format, $particular), array('title' => __('Ver Detalle', true), 'escape' => false)); ?>
            </td>
        </tr>
        <tr>
            <td><b>Eventos</b></td>
            <td><?php echo $stat_event[0][0]['cantidad']!=NULL?$stat_event[0][0]['cantidad']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_event[0][0]['employee_cost']!=NULL?$stat_event[0][0]['employee_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_event[0][0]['services_cost']!=NULL?$stat_event[0][0]['services_cost']:0; ?>&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>$<?php echo $stat_event[0][0]['client_cost']!=NULL?$stat_event[0][0]['client_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_event[0][0]['festilu_profit']!=NULL?$stat_event[0][0]['festilu_profit']:0; ?>&nbsp;</td>
            <td class="actions">
               <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller'=>'jobs','action' => 'index', 'event', $date_from_format, $date_to_format, $particular), array('title' => __('Ver Detalle', true), 'escape' => false)); ?>
            </td>
        </tr>
        <tr class="altrow">
            <td><b>Obras de Teatro</b></td>
            <td><?php echo $stat_theater[0][0]['cantidad']!=NULL?$stat_theater[0][0]['cantidad']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_theater[0][0]['employee_cost']!=NULL?$stat_theater[0][0]['employee_cost']:0; ?>&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>$<?php echo $stat_theater[0][0]['expense_cost']!=NULL?$stat_theater[0][0]['expense_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_theater[0][0]['client_cost']!=NULL?$stat_theater[0][0]['client_cost']:0; ?>&nbsp;</td>
            <td>$<?php echo $stat_theater[0][0]['festilu_profit']!=NULL?$stat_theater[0][0]['festilu_profit']:0; ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller'=>'theaters', 'action' => 'index', $date_from_format, $date_to_format ), array('title' => __('Ver Detalle', true), 'escape' => false)); ?>
            </td>
        </tr>
        <tr>
            <td><b>Gastos Extra</b></td>
            <td><?php echo $stat_expenses[0][0]['cantidad']!=NULL?$stat_expenses[0][0]['cantidad']:0; ?>&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>$<?php echo $stat_expenses[0][0]['value']!=NULL?$stat_expenses[0][0]['value']:0; ?>&nbsp;</td>
            <td>$0&nbsp;</td>
            <td>-$<?php echo $stat_expenses[0][0]['value']!=NULL?$stat_expenses[0][0]['value']:0; ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link($this->Html->image('icons/magnifier.png'), array('controller'=>'expenses', 'action' => 'index', $date_from_format, $date_to_format ), array('title' => __('Ver Detalle', true), 'escape' => false)); ?>
            </td>
        </tr>
        <tr class="tr_total">
            <?php
            $cantidad_total = $stat_birthday[0][0]['cantidad']+ $stat_event[0][0]['cantidad']+$stat_theater[0][0]['cantidad']+$stat_expenses[0][0]['cantidad'];
            $employee_total = $stat_birthday[0][0]['employee_cost']+ $stat_event[0][0]['employee_cost']+$stat_theater[0][0]['employee_cost'];
            $services_total = $stat_birthday[0][0]['services_cost']+$stat_event[0][0]['services_cost'];
            $expense_total = $stat_theater[0][0]['expense_cost']+$stat_expenses[0][0]['value'];
            $client_total = $stat_birthday[0][0]['client_cost']+$stat_event[0][0]['client_cost']+$stat_theater[0][0]['client_cost'];
            $festilu_total = $stat_birthday[0][0]['festilu_profit']+$stat_event[0][0]['festilu_profit']+$stat_theater[0][0]['festilu_profit']-$stat_expenses[0][0]['value'];
            ?>
            <td><b>Total</b></td>
            <td><?php echo $cantidad_total!=NULL?$cantidad_total:0;?></td>
            <td>$<?php echo $employee_total!=NULL?$employee_total:0;?></td>
            <td>$<?php echo $services_total!=NULL?$services_total:0;?></td>
            <td>$<?php echo $expense_total!=NULL?$expense_total:0;?></td>
            <td>$<?php echo $client_total!=NULL?$client_total:0;?></td>
            <td>$<?php echo $festilu_total!=NULL?$festilu_total:0;?></td>
            <td>&nbsp;</td>
        </tr>
    </table>
    <div class="clear"></div>
    <br />
    <div class="resume_total">
        <p><b>Ganancia Festilu = </b> Ganancia - Gastos Extra</p>
        <p><b>Ganancia Festilu = </b> $<?php echo $festilu_total;?></p>
    </div>
</div>
<script type="text/javascript" >

    (function($){
        // Date picker
        $("#StatDateFrom").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });
        // Date picker
        $("#StatDateTo").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

    })(jQuery);
        
    function fecha_hoy(){
        var hoy = $('#StatHiddenActualDate').val();
        $('#StatDateFrom').val(hoy);
        $('#StatDateTo').val(hoy);
        $('#StatGeneralForm').submit();
    }
    
    function fecha_semana(){
        var hoy = $('#StatHiddenActualDate').val();
        var semana = $('#StatHiddenWeekDate').val();
        $('#StatDateFrom').val(semana);
        $('#StatDateTo').val(hoy);
        $('#StatGeneralForm').submit();
    }
</script>